using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;
using System.Threading;
using System.Net;

using DirkRettschlag.Utils;
using System.Diagnostics;

namespace Departure
{
    public partial class frmMain : Form
    {
        Settings settings;
        frmSettings settingsForm;
        frmStationSettings stationSettingsForm;

        SkinSettings skin;

        SortedList<string, ctrlStation> stations;
        SortedList<string, ctrlStationListItem> stationItems;
        List<string> stationItemsOrder;
        string focusedStation;

        bool vga = false;

        ctrlButton selectedButton;
       
        public frmMain()
        {
#if DEBUG           
            Debug.WriteLine("Started: " + DateTime.Now.ToString());

            Debug.WriteLine("Initializing: " + DateTime.Now.ToString());
            
            Stopwatch stopWatch = Stopwatch.StartNew();
#endif
            settings = new Settings();
            settingsForm = new frmSettings(settings);

            
            skin = new SkinSettings(settingsForm.GeneralSettings.Skin + ".skin");
            skin.getSettings();
            
            if (skin.HasLoadErrors)
            {
                settingsForm.GeneralSettings.Skin = "siena";
                skin = new SkinSettings(settingsForm.GeneralSettings.Skin + ".skin");
                skin.getSettings();
            }

            stationSettingsForm = new frmStationSettings();
            stations = new SortedList<string, ctrlStation>();
            stationItems = new SortedList<string, ctrlStationListItem>();
            stationItemsOrder = new List<string>();

            // http://msdn2.microsoft.com/en-us/library/aa446535.aspx

            InitializeComponent();

            if (Platform.GetDeviceType() != "Smartphone")
            {
                InitializeComponentPPC();
            }

            SizeF currentScreen = this.CurrentAutoScaleDimensions;
            if (currentScreen.Height == 192)
            {
                vga = true;
            }
            else
            {
                vga = false;
            }

            ctrlStationList.SkinSettings = skin;
            ctrlStationList.VGA = vga;

            ctrlCountdown.SkinSettings = skin;
            ctrlCountdown.VGA = vga;

            ctrlButtonMainStationList.SkinSettings = skin;
            ctrlButtonMainStationList.VGA = vga;
            ctrlButtonMainStationList.ImagePath = skin.ButtonListPath;
            ctrlButtonMainStationList.Selected = true;
            ctrlButtonMainStationList.Toggled = true;
            selectedButton = ctrlButtonMainStationList;

            ctrlButtonMainCountdown.SkinSettings = skin;
            ctrlButtonMainCountdown.VGA = vga;
            ctrlButtonMainCountdown.ImagePath = skin.ButtonCountdownPath;

            ctrlButtonMainUpdateAll.SkinSettings = skin;
            ctrlButtonMainUpdateAll.VGA = vga;
            ctrlButtonMainUpdateAll.ImagePath = skin.ButtonUpdatePath;

            ctrlButtonMainDunno.SkinSettings = skin;
            ctrlButtonMainDunno.VGA = vga;
            ctrlButtonMainDunno.ImagePath = skin.ButtonEmptyPath;

            ctrlButtonMainMenu.SkinSettings = skin;
            ctrlButtonMainMenu.VGA = vga;
            ctrlButtonMainMenu.ImagePath = skin.ButtonMenuPath;

            ctrlButtonStationBack.SkinSettings = skin;
            ctrlButtonStationBack.VGA = vga;
            ctrlButtonStationBack.ImagePath = skin.ButtonBackPath;

            ctrlButtonStationDepartures.SkinSettings = skin;
            ctrlButtonStationDepartures.VGA = vga;
            ctrlButtonStationDepartures.ImagePath = skin.ButtonListPath;

            ctrlButtonStationCountDown.SkinSettings = skin;
            ctrlButtonStationCountDown.VGA = vga;
            ctrlButtonStationCountDown.ImagePath = skin.ButtonCountdownPath;

            ctrlButtonStationUpdate.SkinSettings = skin;
            ctrlButtonStationUpdate.VGA = vga;
            ctrlButtonStationUpdate.ImagePath = skin.ButtonUpdatePath;

            ctrlButtonStationMenu.SkinSettings = skin;
            ctrlButtonStationMenu.VGA = vga;
            ctrlButtonStationMenu.ImagePath = skin.ButtonMenuPath;

            ctrlStationList.HookKeyDown(this);
            ctrlStationList.IsFocused = true;

            ctrlStationList.Status = "Loading Stations...";

#if DEBUG      
            stopWatch.Stop();

            Debug.WriteLine("Initializing Time:   " + stopWatch.ElapsedMilliseconds.ToString());
      
            Debug.WriteLine("Initialized: " + DateTime.Now.ToString());
#endif
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            //throw new Exception("test");
#if DEBUG
            Debug.WriteLine("Loading: " + DateTime.Now.ToString());
#endif
            timerLoad.Enabled = true;

            SetFullScreen(settingsForm.GeneralSettings.FullScreen);

            tmrRefresh.Interval = Convert.ToInt32(settingsForm.GeneralSettings.UpdateInterval) * 60 * 1000;

            if (settingsForm.GeneralSettings.AutoUpdate)
            {
                tmrRefresh.Enabled = true;
                tmrRefresh_Tick(null, null);
            }

            tmrRemove.Enabled = settingsForm.GeneralSettings.RemovePastDepartures;
#if DEBUG
            Debug.WriteLine("Loaded: " + DateTime.Now.ToString());
#endif
            
        }

        private void frmMain_Closing(object sender, CancelEventArgs e)
        {
            //Cursor.Current = Cursors.WaitCursor;
            //SaveStations();
            //Cursor.Current = Cursors.Default;
        }

        private void frmMain_Resize(object sender, EventArgs e)
        {
            int buttonWidth = this.Width / 5;

            // Main Buttons

            ctrlButtonMainDunno.Left = 0;
            ctrlButtonMainDunno.Width = buttonWidth;

            ctrlButtonMainStationList.Left = ctrlButtonMainDunno.Left + buttonWidth;
            ctrlButtonMainStationList.Width = buttonWidth;

            ctrlButtonMainCountdown.Left = ctrlButtonMainStationList.Left + buttonWidth;
            ctrlButtonMainCountdown.Width = buttonWidth;

            ctrlButtonMainUpdateAll.Left = ctrlButtonMainCountdown.Left + buttonWidth;
            ctrlButtonMainUpdateAll.Width = buttonWidth;

            ctrlButtonMainMenu.Left = ctrlButtonMainUpdateAll.Left + buttonWidth;
            ctrlButtonMainMenu.Width = buttonWidth;

            // Station Buttons

            ctrlButtonStationBack.Left = 0;
            ctrlButtonStationBack.Width = buttonWidth;

            ctrlButtonStationDepartures.Left = ctrlButtonStationBack.Left + buttonWidth;
            ctrlButtonStationDepartures.Width = buttonWidth;

            ctrlButtonStationCountDown.Left = ctrlButtonStationDepartures.Left + buttonWidth;
            ctrlButtonStationCountDown.Width = buttonWidth;

            ctrlButtonStationUpdate.Left = ctrlButtonStationCountDown.Left + buttonWidth;
            ctrlButtonStationUpdate.Width = buttonWidth;

            ctrlButtonStationMenu.Left = ctrlButtonStationUpdate.Left + buttonWidth;
            ctrlButtonStationMenu.Width = buttonWidth;
        }

        private void LoadStations()
        {
            string[] tmpStations = settingsForm.GeneralSettings.Stations.Split('|');
            foreach (string tmpStation in tmpStations)
            {
                if (!String.IsNullOrEmpty(tmpStation))
                    AddStation(tmpStation);
            }

            ctrlStationList.Status = "";
        }

        private void SaveStations()
        {
            for (int i=0; i < stations.Count; i++)
            {
                stations.Values[i].SaveDepartures();
            }

            settingsForm.GeneralSettings.Stations = string.Join("|", stationItemsOrder.ToArray());
            settingsForm.GeneralSettings.saveSettings();

        }

        private void mniExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void mniSettings_Click(object sender, EventArgs e)
        {
            settingsForm.SetStationOrderListView(stationItemsOrder, stationItems);

            if (settingsForm.ShowDialog() == DialogResult.OK)
            {
                tmrRefresh.Enabled = settingsForm.GeneralSettings.AutoUpdate;
                tmrRemove.Enabled = settingsForm.GeneralSettings.RemovePastDepartures;

                skin = new SkinSettings(settingsForm.GeneralSettings.Skin + ".skin");
                skin.getSettings();

                foreach (ctrlStation station in stations.Values)
                {
                    station.SkinSettings = skin;
                }
                ctrlCountdown.SkinSettings = skin;
                ctrlStationList.SkinSettings = skin;
                ctrlButtonMainStationList.SkinSettings = skin;
                ctrlButtonMainStationList.ImagePath = skin.ButtonListPath;
                ctrlButtonMainCountdown.SkinSettings = skin;
                ctrlButtonMainCountdown.ImagePath = skin.ButtonCountdownPath;
                ctrlButtonMainDunno.SkinSettings = skin;
                ctrlButtonMainDunno.ImagePath = skin.ButtonEmptyPath;
                ctrlButtonMainUpdateAll.SkinSettings = skin;
                ctrlButtonMainUpdateAll.ImagePath = skin.ButtonUpdatePath;
                ctrlButtonMainMenu.SkinSettings = skin;
                ctrlButtonMainMenu.ImagePath = skin.ButtonMenuPath;
                ctrlButtonStationBack.SkinSettings = skin;
                ctrlButtonStationBack.ImagePath = skin.ButtonBackPath;
                ctrlButtonStationDepartures.SkinSettings = skin;
                ctrlButtonStationDepartures.ImagePath = skin.ButtonListPath;
                ctrlButtonStationCountDown.SkinSettings = skin;
                ctrlButtonStationCountDown.ImagePath = skin.ButtonCountdownPath;
                ctrlButtonStationUpdate.SkinSettings = skin;
                ctrlButtonStationUpdate.ImagePath = skin.ButtonUpdatePath;
                ctrlButtonStationMenu.SkinSettings = skin;
                ctrlButtonStationMenu.ImagePath = skin.ButtonMenuPath;

                stationItemsOrder = settingsForm.GetStationOrderListView();

                if (settingsForm.GeneralSettings.SortAlphabetically)
                {
                    ctrlStationList.SortStations();
                }
                else
                {
                    ctrlStationList.OrderStations(stationItemsOrder);
                }

                SaveStations();
            }

        }

        private void SetVisibleContent()
        {
            if (this.IsDisposed)
                return;

            if (ctrlCountdown.Visible)
            {
                Cursor.Current = Cursors.Default;
                ctrlCountdown.StartTimer();
            }
            else if (ctrlStationList.Visible)
            {
                Cursor.Current = Cursors.Default;

                foreach (ctrlStation station in stations.Values)
                {
                    station.IsFocused = false;
                }

                ctrlStationList.IsFocused = true;
                ctrlCountdown.StopTimer();

            }
            else if (focusedStation != "" && stations[focusedStation].Visible)
            {
                if (stations[focusedStation].IsUpdating)
                {
                    Cursor.Current = Cursors.WaitCursor;
                    ctrlButtonStationUpdate.ImagePath = skin.ButtonStopPath;
                }
                else
                {
                    Cursor.Current = Cursors.Default;
                    ctrlButtonStationUpdate.ImagePath = skin.ButtonUpdatePath;
                }

                foreach (ctrlStation station in stations.Values)
                {
                    station.IsFocused = false;
                }

                stations[focusedStation].IsFocused = true;
                ctrlStationList.IsFocused = false;
                ctrlCountdown.StopTimer();
            }
            else
            {
                ctrlCountdown.StopTimer();
            }
        }

        private void tmrRefresh_Tick(object sender, EventArgs e)
        {
            for (int i = 0; i < stations.Count; i++)
            {
                if (!stations.Values[i].IsUpdating)
                {
                    stations.Values[i].RefreshDepartures();
                }
            }
        }

        private void tmrRemove_Tick(object sender, EventArgs e)
        {
            if (!backgroundWorkerRemove.IsBusy)
                backgroundWorkerRemove.RunWorkerAsync();
        }

        private void timerLoad_Tick(object sender, EventArgs e)
        {
            timerLoad.Enabled = false;
            LoadStations();
        }

        private void backgroundWorkerRemove_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 0; i < stations.Count; i++)
            {
                if (!stations.Values[i].IsUpdating)
                {
                    stations.Values[i].RemovePastDepartures();
                }
            }
        }
                
        private void backgroundWorkerRemove_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
                throw e.Error;
        }

        private void ctrlCountdown_OnCountdownFinished()
        {
                               
        }

        private void mniAddStation_Click(object sender, EventArgs e)
        {
            AddStation("");

            ctrlStationList.SelectStationItem(focusedStation);

            stations[focusedStation].Visible = true;
            panelStationView.Visible = true;
            panelMainView.Visible = false;
            ctrlStationList.Visible = false;

            SetVisibleContent();
            Application.DoEvents();

            mniStationSettings_Click(this, EventArgs.Empty);
        }

        private void AddStation(string internalName)
        {
            ctrlStation station;

            if (internalName != "")
            {
                station = new ctrlStation(internalName, settingsForm.GeneralSettings, settings, skin, vga);
            }
            else
            {
                station = new ctrlStation("Station" + DateTime.UtcNow.Ticks.ToString(), settingsForm.GeneralSettings, settings, skin, vga);
            }
            
            station.Dock = DockStyle.Fill;
            station.Parent = panelStationContent;
            station.Visible = false;

            station.OnSetRefreshButton += new ctrlStation.SetRefreshButtonEventDelegate(station_OnSetRefreshButton);
            station.OnSetCountdown += new ctrlStation.SetCountdownEventDelegate(station_OnSetCountdown);
            station.HookKeyDown(this);

            string stationName;
            
            if (station.StationSettings.Abbreviation != "")
                stationName = station.StationSettings.Abbreviation;
            else
                stationName = "New Station";

            station.Header = stationName;

            focusedStation = station.Name;

            ctrlStationList.AddStationItem(stationName, station.Name);

            if (settingsForm.GeneralSettings.SortAlphabetically)
                ctrlStationList.SortStations();
            
            lock (stations)
            {
                stations.Add(station.Name, station);
            }

            lock (stationItems)
            {
                stationItems.Add(station.Name, (ctrlStationListItem)ctrlStationList.KListControlStation.GetItemByKey(station.Name));
            }

            lock (stationItemsOrder)
            {
                stationItemsOrder.Add(station.Name);
            }

            station.LoadDepartures();
          
        }

        void station_OnSetCountdown(string countdownTime, string train, string later)
        {
            if (countdownTime != "")
            {
                double correction = 0;
                if (later != "" && later.IndexOf("approx. ") >= 0)
                    correction = double.Parse(later.Replace("approx. ", "").Replace(" minutes later", ""));

                DateTime alarmTime = DateTime.FromFileTimeUtc(long.Parse(countdownTime)).AddMinutes(correction);

                ctrlCountdown.Start(alarmTime, train, later);

                ctrlCountdown.Parent = panelStationContent;
                ctrlCountdown.Visible = true;
                stations[focusedStation].Visible = false;
                ctrlButtonStationDepartures.Toggled = false;
                ctrlButtonStationCountDown.Toggled = true;

                SetVisibleContent();
            }
        }

        void station_OnSetRefreshButton(string internalName, bool enabled)
        {
            if (Invoking.IsVisible(stations[focusedStation]))
            {
                if (enabled)
                {
                    ctrlButtonStationUpdate.ImagePath = skin.ButtonUpdatePath;
                }
                else
                {
                    ctrlButtonStationUpdate.ImagePath = skin.ButtonStopPath;
                }
            }
        }

        private void mniRemoveStation_Click(object sender, EventArgs e)
        {
            if (focusedStation != "")
            {
                if (MessageBox.Show("Do you really want to remove this station?", "Remove Station", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                {
                    lock (stations)
                    {
                        string deletedStation = focusedStation;

                        focusedStation = "";

                        ctrlStationList.Visible = true;
                        panelMainView.Visible = true;
                        panelStationView.Visible = false;

                        File.Delete(Utilities.AppPath() + "\\" + stations[deletedStation].Name + ".xml");

                        stations[deletedStation].StationSettings.removeSettings();
                        stations.RemoveAt(stations.IndexOfKey(deletedStation));

                        stationItems.RemoveAt(stationItems.IndexOfKey(deletedStation));
                        ctrlStationList.RemoveStationItem(deletedStation);
                        stationItemsOrder.Remove(deletedStation);

                        Invalidate();
                    }

                    SaveStations();
                }
            }
        }

        private void mniStationSettings_Click(object sender, EventArgs e)
        {
            if (focusedStation != "")
            {
                stationSettingsForm.StationSettings = stations[focusedStation].StationSettings;
                stationSettingsForm.GeneralSettings = settingsForm.GeneralSettings;
                stationSettingsForm.ShowDialog();

                if (stations[focusedStation].StationSettings.Abbreviation != "")
                {
                    stationItems[focusedStation].Text = stations[focusedStation].StationSettings.Abbreviation;
                    stationItems[focusedStation].StationName = stations[focusedStation].StationSettings.Abbreviation;
                    stations[focusedStation].Header = stations[focusedStation].StationSettings.Abbreviation;
                }

                if (settingsForm.GeneralSettings.SortAlphabetically)
                    ctrlStationList.SortStations();

                stations[focusedStation].SetScaleDimensions();

                SaveStations();
            }
        }

        private void mniFullscreen_Click(object sender, EventArgs e)
        {
            if (this.mniFullscreen.Checked)
                SetFullScreen(false);
            else
                SetFullScreen(true);
        }

        private void SetFullScreen(bool fullScreen)
        {
            if (fullScreen)
            {
                this.WindowState = FormWindowState.Maximized;
                this.stationSettingsForm.WindowState = FormWindowState.Maximized;
                this.settingsForm.WindowState = FormWindowState.Maximized;
            }
            else
            {
                this.WindowState = FormWindowState.Normal;
                this.stationSettingsForm.WindowState = FormWindowState.Normal;
                this.settingsForm.WindowState = FormWindowState.Normal;
            }

            this.mniFullscreen.Checked = fullScreen;
            this.settingsForm.GeneralSettings.FullScreen = fullScreen;

            this.settingsForm.GeneralSettings.saveSettings();
        }

        private void ctrlStationList_OnStationDblClicked(string key)
        {
            focusedStation = key;

            ctrlButtonStationCountDown.Toggled = false;
            ctrlButtonStationDepartures.Toggled = true;

            selectedButton.Selected = false;
            selectedButton = ctrlButtonStationDepartures;
            selectedButton.Selected = true;

            stations[key].Visible = true;
            panelStationView.Visible = true;
            panelMainView.Visible = false;
            ctrlStationList.Visible = false;

            SetVisibleContent();

            Application.DoEvents();
        }

        private void ctrlStationList_OnStationSelected(string key)
        {
            focusedStation = key;
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.KeyCode == System.Windows.Forms.Keys.Left)
            {
                if (selectedButton != null)
                    selectedButton.Selected = false;

                if (selectedButton == ctrlButtonMainDunno)
                {
                    selectedButton = ctrlButtonMainDunno;
                }
                else if (selectedButton == ctrlButtonMainStationList)
                {
                    selectedButton = ctrlButtonMainDunno;
                }
                else if (selectedButton == ctrlButtonMainCountdown)
                {
                    selectedButton = ctrlButtonMainStationList;
                }
                else if (selectedButton == ctrlButtonMainUpdateAll)
                {
                    selectedButton = ctrlButtonMainCountdown;
                }
                else if (selectedButton == ctrlButtonMainMenu)
                {
                    selectedButton = ctrlButtonMainUpdateAll;
                }

                if (selectedButton == ctrlButtonStationBack)
                {
                    selectedButton = ctrlButtonStationBack;
                }
                else if (selectedButton == ctrlButtonStationDepartures)
                {
                    selectedButton = ctrlButtonStationBack;
                }
                else if (selectedButton == ctrlButtonStationCountDown)
                {
                    selectedButton = ctrlButtonStationDepartures;
                }
                else if (selectedButton == ctrlButtonStationUpdate)
                {
                    selectedButton = ctrlButtonStationCountDown;
                }
                else if (selectedButton == ctrlButtonStationMenu)
                {
                    selectedButton = ctrlButtonStationUpdate;
                }

                selectedButton.Selected = true;
            }
            if (e.KeyCode == System.Windows.Forms.Keys.Right)
            {
                if (selectedButton != null)
                    selectedButton.Selected = false;

                if (selectedButton == ctrlButtonMainDunno)
                {
                    selectedButton = ctrlButtonMainStationList;
                }
                else if (selectedButton == ctrlButtonMainStationList)
                {
                    selectedButton = ctrlButtonMainCountdown;
                }
                else if (selectedButton == ctrlButtonMainCountdown)
                {
                    selectedButton = ctrlButtonMainUpdateAll;
                }
                else if (selectedButton == ctrlButtonMainUpdateAll)
                {
                    selectedButton = ctrlButtonMainMenu;
                }
                else if (selectedButton == ctrlButtonMainMenu)
                {
                    selectedButton = ctrlButtonMainMenu;
                }

                if (selectedButton == ctrlButtonStationBack)
                {
                    selectedButton = ctrlButtonStationDepartures;
                }
                else if (selectedButton == ctrlButtonStationDepartures)
                {
                    selectedButton = ctrlButtonStationCountDown;
                }
                else if (selectedButton == ctrlButtonStationCountDown)
                {
                    selectedButton = ctrlButtonStationUpdate;
                }
                else if (selectedButton == ctrlButtonStationUpdate)
                {
                    selectedButton = ctrlButtonStationMenu;
                }
                else if (selectedButton == ctrlButtonStationMenu)
                {
                    selectedButton = ctrlButtonStationMenu; 
                }

                selectedButton.Selected = true;
            }

            base.OnKeyDown(e);

        }

        private void ctrlButton_OnButtonClicked(ctrlButton sender)
        {
            if (selectedButton != null)
                selectedButton.Selected = false;

            selectedButton = sender;

            selectedButton.Selected = true;

            switch (sender.Name)
            {
                case "ctrlButtonMainStationList":
                    ctrlStationList.Visible = true;
                    ctrlCountdown.Visible = false;
                    ctrlButtonMainCountdown.Toggled = false;
                    ctrlButtonMainStationList.Toggled = true;
                    break;
                case "ctrlButtonMainCountdown":
                    ctrlCountdown.Parent = panelMainContent;
                    ctrlCountdown.Visible = true;
                    ctrlStationList.Visible = false;
                    ctrlButtonMainStationList.Toggled = false;
                    ctrlButtonMainCountdown.Toggled = true;
                    break;
                case "ctrlButtonMainUpdateAll":
                    ctrlStationList.OnUpdateStatus("Updating all stations...");
                    foreach (ctrlStation station in stations.Values)
                    {
                        if (!station.IsUpdating)
                        {
                            station.RefreshDepartures();
                        }
                    }
                    break;
                case "ctrlButtonMainDunno":
                    break;
                case "ctrlButtonMainMenu":
                    ctrlButtonMainMenu.Toggled = true;
                    contextMenuMainView.Show(ctrlButtonMainMenu, new Point(0, ctrlButtonMainMenu.Height));
                    ctrlButtonMainMenu.Toggled = false;
                    break;
                case "ctrlButtonStationBack":
                    ctrlButtonMainCountdown.Toggled = false;
                    ctrlButtonMainStationList.Toggled = true;

                    selectedButton.Selected = false;
                    selectedButton = ctrlButtonMainStationList;
                    selectedButton.Selected = true;

                    ctrlStationList.Visible = true;
                    panelMainView.Visible = true;
                    stations[focusedStation].Visible = false;
                    panelStationView.Visible = false;
                    ctrlCountdown.Parent = panelMainContent;
                    ctrlCountdown.Visible = false;
                    break;
                case "ctrlButtonStationDepartures":
                    stations[focusedStation].Visible = true;
                    ctrlCountdown.Visible = false;
                    ctrlButtonStationCountDown.Toggled = false;
                    ctrlButtonStationDepartures.Toggled = true;
                    break;
                case "ctrlButtonStationCountDown":
                    ctrlCountdown.Parent = panelStationContent;
                    ctrlCountdown.Visible = true;
                    stations[focusedStation].Visible = false;
                    ctrlButtonStationDepartures.Toggled = false;
                    ctrlButtonStationCountDown.Toggled = true;
                    break;
                case "ctrlButtonStationUpdate":
                    if (stations[focusedStation].IsUpdating)
                    {
                        stations[focusedStation].StopRefresh();
                    }
                    else
                    {
                        stations[focusedStation].RefreshDepartures();
                    }
                    break;
                case "ctrlButtonStationMenu":
                    contextMenuMainView.Show(ctrlButtonStationMenu, new Point(0, ctrlButtonStationMenu.Height));
                    break;
            }

            SetVisibleContent();
        }

        private void inputPanel_EnabledChanged(object sender, EventArgs e)
        {
            
        }

        private void frmMain_Activated(object sender, EventArgs e)
        {
            inputPanel.Enabled = false;
        }

        private void frmMain_GotFocus(object sender, EventArgs e)
        {
            inputPanel.Enabled = false;
        }


    }

    
}
